Okay.
So today we're going to take a look at module scripts.
Now right at the start when I first started teaching well the first scripting lecture we had in this
course, um, if you did watch that video, you probably remember, um, well, hopefully you remember
me explaining the different types of script.
Now, I mentioned that there are three types of script, but I said the third type model scripts we're
going to leave out for now.
Um, well, we're going to pick them up and learn about them now.
Okay.
So module scripts are a strange type of script really.
Um, don't think of module scripts as similar to scripts and local scripts.
They are quite different.
Okay.
Basically, um, module scripts are very, very useful in when you have much larger games.
So a module script is most useful for, um, larger games or larger projects.
You're doing within a game.
It's not really useful for everyday kind of short scripts, but um, they use to store information such
as functions and tables.
We can have a look at both of those today.
Um, basically storing them in a model script is easier perhaps, than storing them in a server or client
script.
Um, but basically these can be accessed from any script.
Okay.
So you write information in local script which can be accessed anywhere else.
Now these are completely optional.
Okay.
Um, there is never a point where using a model script is essential.
Okay.
You can go throughout your entire scripting journey in Redbook studio without using a single module
script.
It is not essential, but there are cases where they do help a lot.
Um, so definitely is worth looking at them.
So model scripts go inside server script service.
So let's add in here a model script.
And this is what it looks like here.
So how this works is in a model script we have a module which we can save data into.
So we can I'm going to teach you how to different things.
We can put into model script.
We can put in functions and we can put in tables of data.
We'll look at functions first.
So um we're going to drop a couple of lines.
So we're in between here.
And I'm going to create a function.
Now creating a function in a model script is a bit different.
It's not local function like this here.
Um it's a bit different.
Um, the reason why is when you use the local keyword again, think this is one of those things where
I said right at the start, um, oh, you just use local.
It's the, um, key.
It's the keyword making a variable, not.
Keyword you use, it does a bit more to it.
Basically, you use local, um, when you are using that variable or you're using that function in that
script.
Okay.
Um, when you don't use local, it's because that script that that variable or function is being accessed
elsewhere.
Now, because we're writing it in the module script, but accessing it in a different script, we are
not going to use local.
We're going to reference the module and say module dot.
And then we will save a name of our function.
So I'm going to call it my function like that.
But of course you can call it whatever you'd like.
And this function is equal to a new function here which we are going to write out like this.
Okay.
So you can put whatever code you would like inside here to access this.
Let's just print a simple message to make sure it works.
And then we can have a look.
Go and go have a go and look at some more.
Um, a more practical example of this.
So we are referencing the module.
We are naming our function and making that part of the module equal to this function here.
And then we are then afterwards returning the module like that.
This everything we need to do from the module script.
We then need to access this via another script.
So let's add a script into server script service here.
This can be done either on a script or a local script.
Either one you can access the exact same way.
Okay, so we first need to reference the service.
Um the module we need to reference.
So the module is where we've saved our function to.
So to use this we want to make this a variable so local.
Let's call this is my module.
Like they say, if spell it right module is equal to and we're going to use we require function.
And this basically fetches the module from the module script.
So we're going to require the module from the module script.
And to get the module script we just need to reference it.
So game dot server script service um dot module script like that.
There.
Now if you're interested, this is probably one of the only times actually reference server script service.
It's very uncommon for you to reference server script service.
Um, so yeah quite cool.
But yeah.
So once we've referenced our module like that, we then want to get the function from the module.
Now this isn't too difficult because only one function, we want to reference our module and then write
the dots.
And we can see here we'd be able to see here all the function to have accessible.
So in this case it's just for my function there.
So just like that and I have now ran my function.
If I then go and just run the game it will print in the output my letter A because it has.
We've basically run the function from this script which is saved in the module script.
Okay, let's look a bit more of the use of this.
So let's say we have some information on the server and we pass it over to the module script to do stuff
and do some maths calculations to it.
And then we get it back, um, on, on the script.
So let's say for example, let's call this um dot.
Um.
Half round.
Okay.
We're going to do half round.
So what we're going to do here, we're going to half the number and then round the number.
So.
We number we can put pass over here as number as like a parameter here basically.
So um.
Number as a parameter there.
And we're basically going to say, um, local halfte is equal to number.
But if I do, then we can say local rounded is equal to math dot round halved.
Then we can, um, well, we've got a number here, so we'd want to um.
Do something to that heart that round the number.
Let's just print it.
So print rounded just so we can see it appear in the output.
And over on here we can put in any number we'd like sample 58.39 for example.
And if we go ahead and run the game it would halve it and round it to give us 29.
Okay.
So just an example about how you can, um, run some more complicated, longer functions outside of
your main script just to clean up and just clean up things.
Really.
Um, so this could be doing something.
For example, if you want to pass over a player, it might, um, choose that player to be the seeker
for your hide and seek round, stuff like that, which would be quite complicated bit of code.
But by writing all that and accessing it in a different location, it just cleans stuff up.
Really.
Okay, um, is the reason why you might want to use it, um, in your in your game.
Okay.
So this is an example of how you would do it for functions.
I'm also going to explain to you how to do the tables.
That's also a big use of this as well.
So um.
Instead of writing a function, let's write a table.
So I'm going to call this local module for same.
But I'm going to use inside these pair of brackets.
And this is where any items and stuff I'm going to have are.
So, um, let's do this for like some items in the shop for example, so you can list all the items
you have here.
So let's say we're going to list, um, a first item.
So item one like this here okay.
We can then we can basically create almost like a table of tables.
So each item then has is equal to its own table which has other bits of information in there such as
the um, the cost.
Let's say this is equal to 30, for example.
Um, the health.
Of this item 100 and perhaps the damage.
Is equal to 20 like that there.
And you can repeat this for how many items that you have.
Someone just like that.
Item two, item three, and so on.
So instead of storing all of these stats, information and stuff in values or in the actual scripts,
you can store it all in module script.
So it's kind of just separate from your other big your big bulk of code really.
Um, so we then return our module down here again and over in the script, instead of referencing this
function, we can basically just get this module.
So my module dot and we have our three items.
So item 123 let's pick item two.
And then we have all the stats over here for health for example.
And we can access that.
We could print in this case just to show you that it's accessed it correctly.
But in a real game, let's say you're making a shop and um.
You're wanting to display the stats of each item, it can then get it using this method here, and then
display it as a GUI or anything else like that.
Okay, so and it's printed 100 that what it was.
So model scripts are really cool.
Honestly.
Do I use them?
Um, not particularly I'll be honest.
Um, I have used, I've used model scripts in games and stuff before, but I don't use them too regularly.
Um, the reason why is I think, um.
There are well for the application that it does have.
For example, for the shop, there are other methods you can use instead of module scripts.
I think it's more of a personal preference.
Some people.
Very high game on Mondays with any any function they'll create.
We put inside a model script and any, any big block of code, make a function and put it in the model
script.
Um, other people like me just prefer not really to use them.
It's more of a preference thing, but you do still need to know how they work, regardless of if you
want to use them or not.
Um, just for sake of, um, let's say you're working, you'll want to work as a scripter, um, in
a developer team, for example, and the head developer, whatever, has decided to use model scripts.
Um, for this, you need to know how to handle those and be able to access those and use those.
Also, if you're wanting to do something like edit a script you found in the toolbox, it contains model
scripts.
You need to know how to access and use those.
So even if you're not planning on using them, make sure you're still kind of familiar with them.
But they're more just as an option there.
And again, they're not absolutely not essential.
Um, it's only really if you want to use them, I think they're really good.
And we will probably look at some applications of this in our game in the next section.
But um, we'll have a look and find out if that's the case or not later on.
Um, so have a practice of those and have a look at the applications of them and join me in the next
lecture where we are going to have a look at how to carry out pathfinding was really cool.
So join me in that next lecture.